<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use think\facade\Filesystem;
use think\facade\Db;

class Common extends Base
{
  /**
   * 文件上传方法
   * @return [type] [description]
   */
  public function upload($module='admin',$use='admin_img')
  {
    if($this->request->file('file')){
      $file = $this->request->file('file');
    }else{
      $res['code']=1;
      $res['msg']='没有上传文件';
      return json($res);
    }
    $module = $this->request->has('module') ? $this->request->param('module') : $module;//模块
    $system = Db::name('system')->where('web','web')->find();
    try {
      validate(['file' => [
        'fileSize'=>$system['file_size']*1024,
        'fileExt'=>$system['file_type']],
        'fileMime' => 'image/jpeg,image/png,image/gif',
      ])
      ->check(['file' => $file]);
    } catch (ValidateException $e) {
      return error($e->getMessage());
    }

    $info = Filesystem::disk('public')->putFile('\uploads'.'\\' .$module, $file);
    if($info) {
      //写入到附件表
      $data = [];
      $data['module'] = $module;
      $data['url'] = $info;//文件路径
      $data['create_time'] = time();//时间
      $data['uploadip'] = $this->request->ip();//IP
      if($data['module'] = 'admin') {
        //通过后台上传的文件直接审核通过
        $data['status'] = 1;
      }
      $data['use'] = $this->request->has('use') ? $this->request->param('use') : $use;//用处
      $res['id'] = Db::name('images')->insertGetId($data);
      $res['src'] = $info;
      $res['code'] = 2;
      adminLog();//记录日志
      return json($res);
    } else {
      // 上传失败获取错误信息
      return $this->error('上传失败：'.$file->getError());
    }
  }

  /**
   * 清除缓存
   * @return [type] [description]
   */
  public function clear() {

  }


  
}

?>